<%@Page Language="C#"%>
<%@ Import Namespace="System.IO" %>

<script runat=server>

	Hashtable shoppingCart;

	protected void Page_Load( object sender, EventArgs e )
	{

//		if ( ViewState["cart"] == null )
//		{
//			shoppingCart = new Hashtable();
//			ViewState["cart"] = shoppingCart;
//		}
//		else
//		{
//			shoppingCart = (Hashtable) ViewState["cart"];
//		}

		if ( IsPostBack == true )
		{
			shoppingCart = (Hashtable) Session["cart"];
		}
		else
		{
			shoppingCart = new Hashtable();
			Session["cart"] = shoppingCart;
		}

	}

	// Add an item to the cart 
	protected void OnAddItemToCart( object sender, EventArgs e )
	{
		if ( shoppingCart.ContainsKey( ProductCode.Text ) == true )
		{
			status.InnerHtml = "Product code already in cart";
		}
		else
		{
			shoppingCart.Add( ProductCode.Text, ProductDescription.Text );
		}
		
	}
	
	// Read an item from the cart 
	protected void OnReadItemFromCart( object sender, EventArgs e )
	{
		if ( shoppingCart.ContainsKey( ProductCode.Text ) == false )
		{
			status.InnerHtml = "Product code not in cart";
			return;
		}

		if ( shoppingCart[ProductCode.Text] != null )
		{
			ProductDescription.Text = (string) shoppingCart[ProductCode.Text];
		}
		
	}
	// Delete an item from the cart
	protected void OnDeleteItemFromCart( object sender, EventArgs e )
	{
		if ( shoppingCart.ContainsKey( ProductCode.Text ) == false )
		{
			status.InnerHtml = "Product code not in cart";
			return;
		}

		shoppingCart.Remove( ProductCode.Text );
		
	}

	// Updated an item in the cart
	protected void OnUpdateItemInCart( object sender, EventArgs e )
	{
		if ( shoppingCart.ContainsKey( ProductCode.Text ) == false )
		{
			status.InnerHtml = "Product code not in cart";
			return;
		}

		shoppingCart[ProductCode.Text] = ProductDescription.Text;
	
	}


</script>

<form runat=server>

<h3>Items in Cart</h3>

<%

	if ( shoppingCart.Count != 0 ) 
	{
	
		Response.Write("<table>");
		Response.Write("<tr bgcolor=cornflowerblue>");
		Response.Write("<td>Product Code");
		Response.Write("<td>Product Description");
		Response.Write("</tr>");

		foreach( string key in shoppingCart.Keys )
		{
			Response.Write("<tr>");
			Response.Write("<td>" + key );
			Response.Write("<td>" + (string) shoppingCart[key] );
		}
		Response.Write("</table>");
	}
	else
	{
		Response.Write("<P>There are no items in the cart at the moment");
	}
%>

<hr />
<h3>Add/Delete/Update Item</h3>

<table>
<tr>
	<td>Code:</td>
	<td><asp:TextBox id=ProductCode runat=server EnableViewState=False />
<tr>
	<td>Description:</td>
	<td><asp:TextBox id=ProductDescription runat=server EnableViewState=False />

<tr>
	<td colspan=2>
		<asp:Button OnClick="OnAddItemToCart" Text="Add Item" runat=server />
		<asp:Button OnClick="OnReadItemFromCart" Text="Read Item" runat=server />
		<asp:Button OnClick="OnDeleteItemFromCart" Text="Delete Item" runat=server />
		<asp:Button OnClick="OnUpdateItemInCart" Text="Update Item" runat=server />
</table>

</form>

<p style="color:red" id=status EnableViewState=False runat=server />
